//  Blitz image vision common utility implementation
//

//  Copyright Jeff Wong 2011
#include "stdafx.h"
#include "BlitzUtility.h"
#include <cstdlib>
#include <cassert>

#ifdef __cplusplus
namespace geometry {
#endif

void* aligned_malloc(size_t size, size_t align)
{
	assert(align>0 && PowerOf2(align));

	void* p = malloc(size + align - 1 + sizeof(void*));

	if(p == NULL) return NULL;

	void* ptr = (void*)(((unsigned __int64)p + sizeof(void*) + align - 1) & ~(align-1));
	*((void**)ptr - 1) = p;

	return ptr;
}

void aligned_free(void* memblock)
{
	if(memblock) free(*((void**)memblock - 1));
}

#ifdef __cplusplus
}
#endif